﻿using Company.Sqlite.Models;
using SQLite.CodeFirst;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Sqlite
{
    public class SqliteDbContext : DbContext
    {
        // 读取配置文件connectionStrings, 创建数据库映射
        public SqliteDbContext():base("SqliteDbContext")
        { 
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Models.Company> Companys { get; set; }
        public DbSet<Stuff> Stuffs { get; set; }
        public DbSet<Car> Cars { get; set; }
        public DbSet<Data> Datas { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 注入我们设置的SqlliteDbContext, 判断数据库是否存在, 不存在则创建
            var sqliteConnect = new SqliteCreateDatabaseIfNotExists<SqliteDbContext>(modelBuilder);

            // 注入我们设置的SqlliteDbContext, 判断数据模型是否改变, 如果改变了在则删除数据库重新创建
            //var sqliteConnect = new SqliteDropCreateDatabaseWhenModelChanges<SqliteDbContext>(modelBuilder);

            // 执行
            Database.SetInitializer(sqliteConnect);
        
        }
    }
}
